package com.mz.ozy.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.github.penfeizhou.animation.apng.APNGDrawable;
import com.github.penfeizhou.animation.loader.AssetStreamLoader;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.utils.AppManager;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.widget.LoadingDialog;
import com.mz.ozy.widget.ob.DetailScrollStateListener;
import com.trello.rxlifecycle2.components.support.RxFragment;

import java.lang.reflect.ParameterizedType;

import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;

/**
 *
 */

public abstract class BaseFragment<V extends BaseView, P extends BasePresenterImpl<V>> extends RxFragment implements BaseView {

    protected Activity mActivity;
    LoadingDialog kProgressHUD;
    public P mPresenter;

    protected DetailScrollStateListener mScrollStateListener;
    protected boolean canTouch = true;


    protected void updateCanTouch(boolean canTouch){
        this.canTouch = canTouch;
    }
    public boolean canChildTouch() {
        return canTouch;
    }
    protected RecyclerView.OnScrollListener getOnScrollListener(){
        return new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(!recyclerView.canScrollVertically(-1)){//在顶部，且当前View已经允许可滑动
                    if(dy < 0){//向上滑，应该直接滑动父容器
                        updateCanTouch(false);
                    }else{//向下滑，直接滑动
                        updateCanTouch(true);
                    }
                }else{
                    updateCanTouch(true);
                }
            }
        };
    }

    protected NestedScrollView.OnScrollChangeListener getNestedScrollListener(){
        return new NestedScrollView.OnScrollChangeListener() {

            @Override
            public void onScrollChange(NestedScrollView nestedScrollView, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if(!nestedScrollView.canScrollVertically(-1)){//在顶部，且当前View已经允许可滑动
                    if(scrollY < 0){//向上滑，应该直接滑动父容器
                        updateCanTouch(false);
                    }else{//向下滑，直接滑动
                        updateCanTouch(true);
                    }
                }else{
                    updateCanTouch(true);
                }
            }
        };
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        kProgressHUD = new LoadingDialog(getActivity());
        mPresenter = getInstance(this, 1);
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
        if(getActivity() instanceof  DetailScrollStateListener){
            mScrollStateListener = (DetailScrollStateListener) getActivity();
        }
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return null;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void onStop() {
        super.onStop();
        dismissLoading();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
        if (kProgressHUD != null) {
            kProgressHUD.dismissLoading();
            kProgressHUD = null;
        }
    }

    //反射获取当前Presenter对象
    public <M> M getInstance(Object o, int i) {
        try {
            return ((Class<M>) ((ParameterizedType) (o.getClass().getGenericSuperclass())).getActualTypeArguments()[i]).newInstance();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void showLoading() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (kProgressHUD.isShow()) {
                    return;
                }
                kProgressHUD.showLoading();
            }
        });
    }

    public void dismissLoading() {
        if (kProgressHUD != null && kProgressHUD.isShow()) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    kProgressHUD.dismissLoading();
                }
            });
        }
    }

    public void handleFailResponse(NetBean bean) {
        if (bean.getRespCode().equals(Constants.APP_NOT_INIT)) {
            AppManager.getAppManager().goLoginActivity(mActivity);

        } else {
            ToastUtils.showToast(bean.getRespMsg());
        }
    }

}
